package com.iwords.android;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.iwords.android.DBHelper.LearningStatus;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

public class WordsQueueManager {
	private static int KNOWN_INDEX = 0;// KnownLevel.Known
	private static int LEVEL5_INDEX = 1;// KnownLevel.Level5
	private static int UNKNOWN_INDEX = 6;// KnownLevel.UnKnown
	/** 用户上一次垂直操作的方向，用于确定下一个应该显示的单词 */
	private Boolean vDown = Boolean.FALSE;
	/** 当前单词队列的序列，默认值是1，也就是记忆周期在15天的单词，即将进入永久记忆周期的队列。序列为0的是knownlevel，不显示。 */
	private int currentQueueIndex = -1;

	private IWordsApp iWordsApp;
	private static WordsQueueManager INSTANCE;
	private static LinkedList<WordStatus> CurrentQueue = new LinkedList<WordStatus>();
	private Long currentLibId;
	private String currentLibName;
	private Activity activity;

	private WordsQueueManager() {
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public Long getCurrentLibId() {
		return currentLibId;
	}

	public String getCurrentLibName() {
		return currentLibName;
	}

	public void setCurrentLibName(String currentLibName) {
		this.currentLibName = currentLibName;
	}

	public void setCurrentLibId(Long currentLibId) {
		this.currentLibId = currentLibId;
	}

	public static WordsQueueManager getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new WordsQueueManager();
		}
		return INSTANCE;
	}

	public void setIWordsApp(IWordsApp iWordsApp) {
		this.iWordsApp = iWordsApp;
	}

	/**
	 * 设置当前的队列
	 */
	void setCurrentQueueIndex(int currentQueueIndex) {
		//System.out.println("WordsQueueManager.setCurrentQueueIndex() currentQueueIndex = " + currentQueueIndex);
		this.currentQueueIndex = currentQueueIndex;
	}

	int getCurrentQueueIndex() {
		return this.currentQueueIndex;
	}

	/** 获取默认应该第一个显示的队列的待显示单词 */
	WordStatus getDefaultWord() throws Exception{
		//System.out.println("WordsQueueManager.getDefaultWord() start currentQueueIndex = " + currentQueueIndex);
		for (int i = 1; i <= UNKNOWN_INDEX; i++) {
			this.currentQueueIndex = i;
			if (iWordsApp.getCurrentLibStatus().getGreenCount(i) > 0) {
				//System.out.println("WordsQueueManager.getDefaultWord() end currentQueueIndex = " + currentQueueIndex);
				this.loadCurrentQueue();
				break;
			}
		}
		return getWord();
	}

	/** 获取当前队列的待显示单词 */
	WordStatus getWord() throws Exception{
		//System.out.println("WordsQueueManager.getWord() end currentQueueIndex = " + currentQueueIndex);
		//System.out.println("WordsQueueManager.getWord() getCurrentQueue().size() = " + getCurrentQueue().size());
		if (this.currentQueueIndex == -1 || getCurrentQueue().size() < 1){
			if(this.getCurrentQueueIndex() == 6){//只有点中的是新词队列，才需要问用户要不要再来一批
				throw new NeedMoreNewWordsException("Need More.");
			}else{
				throw new Exception("当前等级没有待复习词汇。");
			}
		}
		if (this.vDown) {
			WordStatus word = getCurrentQueue().removeLast();
			getCurrentQueue().addFirst(word);
			return word;
		} else {
			WordStatus word = getCurrentQueue().removeFirst();
			getCurrentQueue().addLast(word);
			return word;
		}
	}

	WordStatus flingUp() {
		if (getCurrentQueue().size() == 0)
			return null;
		if (this.vDown) {
			getCurrentQueue().addLast(getCurrentQueue().removeFirst());
		}
		WordStatus word = getCurrentQueue().removeFirst();
		getCurrentQueue().addLast(word);
		this.vDown = Boolean.FALSE;
		return word;
	}

	WordStatus flingDown() {
		if (getCurrentQueue().size() == 0)
			return null;
		if (!this.vDown) {
			getCurrentQueue().addFirst(getCurrentQueue().removeLast());
		}
		WordStatus word = getCurrentQueue().removeLast();
		getCurrentQueue().addFirst(word);
		this.vDown = Boolean.TRUE;
		return word;
	}

	/**
	 * 向左移动：
	 * familarity=7 也就是Done，只允许当天update为7的词汇左移，用作新词误操作场景试用。
	 */
	WordStatus flingLeft() {
		//System.out.println("WordsQueueManager.flingLeft() getCurrentQueue().size() = " + getCurrentQueue().size());
		if (getCurrentQueue().size() == 0)
			return null;
		WordStatus word = null;
		WordStatus lastWord = null;
		if (this.vDown) {
			// 把当前显示的词汇，移动到左边队列
			lastWord = getCurrentQueue().removeFirst();
			this.increaseLeft();
			// 如果是将单词移动到最左侧，则还需要将该单词加入到用户词汇量中

			// System.out.println("WordsQueueManager.flingLeft() 向左滑动 被移动到左边的单词是 "+lastWord.getSpelling());
			// System.out.println("WordsQueueManager.flingLeft() 当前队列的单词数 getCurrentQueue().size()="+getCurrentQueue().size());
			// 取出下一个词汇，待显示
			if (getCurrentQueue().size() > 0) {
				word = getCurrentQueue().removeLast();
				getCurrentQueue().addFirst(word);
				// System.out.println("WordsQueueManager.flingLeft() 向左滑动 待显示的下一个单词是 "+word.getSpelling());
			}
		} else {
			// 先将正在显示的单词，移动到左侧队列
			lastWord = getCurrentQueue().removeLast();
			this.increaseLeft();
			// System.out.println("WordsQueueManager.flingLeft() 向左滑动 被移动到左边的单词是 "+lastWord.getSpelling());
			// System.out.println("WordsQueueManager.flingLeft() 当前队列的单词数 getCurrentQueue().size()="+getCurrentQueue().size());

			// 再从当前队列取出待显示的单词
			if (getCurrentQueue().size() > 0) {
				word = getCurrentQueue().removeFirst();
				getCurrentQueue().addLast(word);
				// System.out.println("WordsQueueManager.flingLeft() 向左滑动 待显示的下一个单词是 "+word.getSpelling());
			}
		}

		this.updateWordStatus(lastWord, getLeftKnownLevel());
		return word;
	}

	/**
	 * 向右移动：
	 * 新词向右移动，直接进入Done。
	 * Done队列只加载当天update为7的单词，Done队列向右移动，update为6。
	 * 如果是其它状态右移
	 */
	WordStatus flingRight() {
		// System.out.println("WordsQueueManager.flingRight() getCurrentQueue().size() = "
		// + getCurrentQueue().size());
		if (getCurrentQueue().size() == 0)
			return null;
		WordStatus word;
		WordStatus lastWord;
		if (this.vDown) {
			lastWord = getCurrentQueue().removeFirst();
			this.increaseRight();
			this.updateWordStatus(lastWord, getRightKnownLevel());

			if (getCurrentQueue().size() == 0)
				return null;
			word = getCurrentQueue().removeLast();
			getCurrentQueue().addFirst(word);
			return word;
		} else {
			// 先将正在显示的单词，移动到右侧队列
			lastWord = getCurrentQueue().removeLast();
			this.increaseRight();
			this.updateWordStatus(lastWord, getRightKnownLevel());

			// 再从当前队列取出待显示的单词
			if (getCurrentQueue().size() == 0)
				return null;
			word = getCurrentQueue().removeFirst();
			getCurrentQueue().addLast(word);
			return word;
		}
	}

	/**
	 * 获取当前队列左边的队列，最左边的左边是最右边
	 */
	private void increaseLeft() {
		//System.out.println("************************WordsQueueManager.increaseLeft() this.currentQueueIndex = "+ this.currentQueueIndex);
		if (this.currentQueueIndex - 1 < 0) {// 最左边的左边是最右边
			iWordsApp.getCurrentLibStatus().increaseRed(UNKNOWN_INDEX);
		} else {
			iWordsApp.getCurrentLibStatus().increaseRed(this.currentQueueIndex - 1);
			if (this.currentQueueIndex == LEVEL5_INDEX) {// 如果是第15天的队列，则既要增加red数字又要增加green数字
				iWordsApp.getCurrentLibStatus().increaseGreen(KNOWN_INDEX);
			}
		}
		iWordsApp.getCurrentLibStatus().decreaseGreen(this.currentQueueIndex);
	}

	/**
	 * 获取当前队列右边的队，最右边的右边是最左边
	 */
	private void increaseRight() {
		//System.out.println("************************WordsQueueManager.increaseRight() this.currentQueueIndex = "+ this.currentQueueIndex);
		if (this.currentQueueIndex + 1 > UNKNOWN_INDEX) {
			iWordsApp.getCurrentLibStatus().increaseGreen(KNOWN_INDEX);// 最右边的右边是最左边，也就是单词直接进Known的情况
			iWordsApp.getCurrentLibStatus().increaseRed(KNOWN_INDEX);// Known队列上下数字各+1
		} else {
			iWordsApp.getCurrentLibStatus().increaseRed(this.currentQueueIndex + 1);//除了最右边的右移情况之外的情形，都需要将右侧对列红色数字＋1
		}
		//如果是Done队列右移，还应该将当前队列的红色数字－1
		if(this.currentQueueIndex == KNOWN_INDEX){
			iWordsApp.getCurrentLibStatus().decreaseRed(KNOWN_INDEX);
		}
		iWordsApp.getCurrentLibStatus().decreaseGreen(this.currentQueueIndex);
	}

	/**
	 * 获取当前队列左边的队列的KnownLevel 最左边的左边是最右边
	 */
	private KnownLevel getLeftKnownLevel() {
		// System.out.println("************************WordsQueueManager.getLeftKnownLevel() this.currentQueueIndex = "+this.currentQueueIndex);
		if (this.currentQueueIndex - 1 < 0) {
			return iWordsApp.getKnownLevel(iWordsApp.getKnownLevelsSize() - 1);// 最左边的左边是最右边
		} else {
			return iWordsApp.getKnownLevel(this.currentQueueIndex - 1);
		}
	}

	/**
	 * 获取当前队列右边的队列的KnownLevel 最右边的右边是最左边
	 */
	private KnownLevel getRightKnownLevel() {
		// System.out.println("************************WordsQueueManager.getRightKnownLevel() this.currentQueueIndex = "+this.currentQueueIndex);
		if (this.currentQueueIndex + 1 >= iWordsApp.getKnownLevelsSize()) {
			return iWordsApp.getKnownLevel(0);// 最右边的右边是最左边
		} else {
			return iWordsApp.getKnownLevel(this.currentQueueIndex + 1);
		}
	}

	/** 获取当前待复习队列 */
	LinkedList<WordStatus> getCurrentQueue() {
		return WordsQueueManager.CurrentQueue;
	}
	
	public void initLibName() {
		DBHelper dbHelper = new DBHelper(activity);
		long userid = iWordsApp.getUser().getUserid();
		try{
			DBHelper.UserLibs userLib = dbHelper.getLib(this.currentLibId, userid);
			this.setCurrentLibName(userLib.libname);
		}finally{
			dbHelper.cleanup();
		}
	}
	/** 从服务器端获取用户当前词库的信息，为界面初始化提供信息
	public void initLibName() {
		String href = iWordsApp.getResources().getString(R.string.book_detail);// 获取课本列表
		href = href + this.currentLibId;
		HttpPost request = new HttpPost(href);
		// System.out.println("WordsQueueManager.initLibName()! href="+href);
		try {
			JSONObject jsonObject = ServerAccessor.access(activity, request);
			boolean success = jsonObject.getBoolean("isSuccess");
			if (success) {
				JSONObject jsonLib = jsonObject.getJSONObject("defaultValue");
				this.setCurrentLibName(CodeUtils.decode(jsonLib.getString("libname")));
			} else {
				this.displayMessage(CodeUtils.decode(jsonObject.getString("message")));
			}
		} catch (NeedPayException ne) {
			Intent intent = new Intent(activity, com.iwords.android.AlipayActivity.class);
			activity.startActivity(intent);
		} catch (Exception e) {
			this.displayMessage(e.getMessage());
		}
	} */
	
	void clearCurrentQueue(){
		CurrentQueue.clear();
	}
	
	static final long Millis_Per_Day = 60 * 60 * 24 * 1000L;
	/**加载当前队列的待复习词汇---从本地数据库中*/
	void loadCurrentQueue() {
		//System.out.println("WordsQueueManager.loadCurrentQueue() .....");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
		CurrentQueue.clear();// 不管如何，逻辑走到这里，应该先清理当前Queue中的数据。
		//System.out.println("WordsQueueManager.loadCurrentQueue() .....this.currentQueueIndex = "+this.currentQueueIndex);
		//System.out.println("WordsQueueManager.loadCurrentQueue() current KnownLevel="+iWordsApp.getKnownLevel(this.currentQueueIndex));
		//System.out.println("WordsQueueManager.loadCurrentQueue() current KnownLevel.getRetainDays()="+iWordsApp.getKnownLevel(this.currentQueueIndex).getRetainDays());
		if (this.currentQueueIndex != -1) {
			int familiarity = iWordsApp.getKnownLevel(this.currentQueueIndex).getValue();
			DBHelper dbHelper = new DBHelper(activity);
			long userid = iWordsApp.getUser().getUserid();
			try{
				//System.out.println("WordsQueueManager.loadCurrentQueue() .....this.currentLibId = "+this.currentLibId);
				KnownLevel knownLevel = iWordsApp.getKnownLevel(this.currentQueueIndex);
				List<LearningStatus> learningStatusList = dbHelper.getLearningStatus(userid, this.currentLibId, familiarity);
				for(LearningStatus status : learningStatusList){
					WordStatus wordStatus = new WordStatus();
					wordStatus.setFamiliarity(status.familiarity);
					wordStatus.setId(status.id);
					wordStatus.setLibId(status.libid);
					wordStatus.setUserid(status.userid);
					wordStatus.setWordId(status.wordid);
					wordStatus.setSpelling(status.spelling);
					wordStatus.setMeaning(status.meaning);
					wordStatus.setUpdateTime(status.updateTime.getTime());
					wordStatus.setPhonetics(status.phonetics);
					//如果是新词，全部添加，否则应该只添加到期的可复习词汇
					//System.out.println("WordsQueueManager.loadCurrentQueue() status.updateTime="+sdf.format(status.updateTime));
					if(knownLevel.equals(KnownLevel.Unknown)){
						CurrentQueue.add(wordStatus);
					}else if(knownLevel.equals(KnownLevel.Known)){//如果是Done队列，只有当天加入的词汇可以被看到
						if(System.currentTimeMillis() - status.updateTime.getTime() < Millis_Per_Day){
							CurrentQueue.add(wordStatus);
						}
					}else if(dbHelper.green(knownLevel, status)){
						//System.out.println("knownLevel.getRetainDays() * Millis_Per_Day = "+knownLevel.getRetainDays() * Millis_Per_Day);
						//System.out.println("System.currentTimeMillis() =" + System.currentTimeMillis());
						//System.out.println("status.updateTime.getTime() =" + status.updateTime.getTime());
						//System.out.println("System.currentTimeMillis() - status.updateTime.getTime() = "+(System.currentTimeMillis() - status.updateTime.getTime()));
						CurrentQueue.add(wordStatus);
					}
				}
				//System.out.println("WordsQueueManager.loadCurrentQueue() CurrentQueue.size() = "+CurrentQueue.size());
			}finally{
				dbHelper.cleanup();
			}
		}
	}
	/**加载当前队列的待复习词汇
	void loadCurrentQueue() {
		CurrentQueue.clear();// 不管如何，逻辑走到这里，应该先清理当前Queue中的数据。
		if (this.currentQueueIndex != -1) {
			String href = iWordsApp.getResources().getString(R.string.user_lib_flevel);
			int familiarityLevel = iWordsApp.getKnownLevel(this.currentQueueIndex).getValue();
			href = href + "&userid=" + iWordsApp.getUser().getUserid() + "&libid=" + this.currentLibId
					+ "&familiarity=" + familiarityLevel;
			HttpPost request = new HttpPost(href);
			System.out.println("---WordsQueueManager.loadCurrentQueue()! href = "+ href);
			try {

				JSONObject jSONObject = ServerAccessor.access(activity, request);
				boolean success = jSONObject.getBoolean("isSuccess");
				if (!success) {
					this.displayMessage(CodeUtils.decode(jSONObject.getString("message")));
					return;
				}
				JSONArray wordStatusList = JSONUtils.getArray(jSONObject.getJSONObject("defaultValue"));
				//System.out.println("---WordsQueueManager.loadCurrentQueue()! wordStatusList = " + wordStatusList.length());
				for (int i = 0; i < wordStatusList.length(); i++) {
					JSONObject jsonObject = wordStatusList.getJSONObject(i);
					WordStatus wordStatus = new WordStatus();
					Integer familiarity = jsonObject.getInt("familiarity");
					wordStatus.setFamiliarity(familiarity);
					wordStatus.setId(jsonObject.getLong("id"));
					wordStatus.setLibId(jsonObject.getLong("libid"));
					wordStatus.setUserid(jsonObject.getLong("userid"));
					wordStatus.setWordId(jsonObject.getLong("wordid"));
					wordStatus.setSpelling(CodeUtils.decode(jsonObject.getString("spelling")));
					wordStatus.setMeaning(CodeUtils.decode(jsonObject.getString("meaning")));
					wordStatus.setUpdateTime(jsonObject.getLong("updatetime"));

					if (jsonObject.has("phonetics")) {
						wordStatus.setPhonetics(CodeUtils.decode(jsonObject.getString("phonetics")));
					}
					CurrentQueue.add(wordStatus);
				}
			} catch (NeedPayException ne) {
				Intent intent = new Intent(activity, com.iwords.android.AlipayActivity.class);
				activity.startActivity(intent);
			} catch (Exception e) {
				this.displayMessage(e.getMessage());
			}
		}
	}*/
	void newBatch() throws Exception {
		DBHelper dbHelper = new DBHelper(activity);
		long userid = iWordsApp.getUser().getUserid();
		try{
			//DBHelper.UserLibs userLib = dbHelper.getLib(this.currentLibId, userid);
			dbHelper.newBatch(userid, this.currentLibId, iWordsApp.getUserSettings().optInt("batch_number", 20));
			loadStatus();
		}finally{
			dbHelper.cleanup();
		}
	}
	/** 加载当前词库的所有词汇库---从本地*/
	boolean loadStatus() throws Exception {
		iWordsApp.clear();
		this.initLibName();
		//System.out.println("WordsQueueManager.loadStatus() ");
		DBHelper dbHelper = new DBHelper(activity);
		long userid = iWordsApp.getUser().getUserid();
		try{
			DBHelper.UserLibs userLib = dbHelper.getLib(this.currentLibId, userid);
			//System.out.println("WordsQueueManager.loadStatus() userLib="+userLib);
			//检查是否需要将其中的一部分词汇，设置为待学习词汇
			if(userLib!=null){
				//只要超过10小时，就允许获得另一批单词进行学习
				//System.out.println("WordsQueueManager.loadStatus() userLib.lastBatchTime="+userLib.lastBatchTime);
				if(userLib.lastBatchTime==null || userLib.lastBatchTime.getTime()<(System.currentTimeMillis()-10*60*60*1000)){
					dbHelper.newBatch(userid, this.currentLibId, iWordsApp.getUserSettings().optInt("batch_number", 20));
					//System.out.println("WordsQueueManager.loadStatus() 需要另外一批新词");
				}else{
					//System.out.println("WordsQueueManager.loadStatus() 不需要另外一批新词");
				}
			}
			int[][] levelNums = dbHelper.getLevelNums(userid, this.currentLibId);
			WordsLibStatus status = new WordsLibStatus();
			iWordsApp.setCurrentLibStatus(status);
			for(int i=0; i<levelNums[0].length; i++){
				status.getRedCounts()[i] = levelNums[0][i];
			}
			for(int i=0; i<levelNums[1].length; i++){
				status.getGreenCounts()[i] = levelNums[1][i];
			}
		}finally{
			dbHelper.cleanup();
		}
		return true;
	}
	/** 加载当前词库的所有词汇库
	boolean loadStatus() throws Exception {
		iWordsApp.clear();
		this.initLibName();
		String href = iWordsApp.getResources().getString(R.string.user_lib_status);
		href = href + "&userid=" + iWordsApp.getUser().getUserid() + "&libid=" + this.currentLibId;
		HttpPost request = new HttpPost(href);
		//System.out.println("---WordsQueueManager.loadStatus()! href = " + href);
		try {
			JSONObject jSONObject = ServerAccessor.access(activity, request);
			//System.out.println("---WordsQueueManager.loadStatus()! jSONObject = " + jSONObject);
			boolean success = jSONObject.getBoolean("isSuccess");
			if (!success) {
				this.displayMessage(CodeUtils.decode(jSONObject.getString("message")));
				return false;
			}
			JSONObject libStatus = jSONObject.getJSONObject("defaultValue");
			WordsLibStatus status = new WordsLibStatus();
			iWordsApp.setCurrentLibStatus(status);
			if (libStatus.has("known_u")) {
				status.getGreenCounts()[0] = libStatus.getInt("known_u");
			}
			if (libStatus.has("known_d")) {
				status.getRedCounts()[0] = libStatus.getInt("known_d");
			}
			if (libStatus.has("level5_u")) {
				status.getGreenCounts()[1] = libStatus.getInt("level5_u");
			}
			if (libStatus.has("level5_d")) {
				status.getRedCounts()[1] = libStatus.getInt("level5_d");
			}
			if (libStatus.has("level4_u")) {
				status.getGreenCounts()[2] = libStatus.getInt("level4_u");
			}
			if (libStatus.has("level4_d")) {
				status.getRedCounts()[2] = libStatus.getInt("level4_d");
			}
			if (libStatus.has("level3_u")) {
				status.getGreenCounts()[3] = libStatus.getInt("level3_u");
			}
			if (libStatus.has("level3_d")) {
				status.getRedCounts()[3] = libStatus.getInt("level3_d");
			}
			if (libStatus.has("level2_u")) {
				status.getGreenCounts()[4] = libStatus.getInt("level2_u");
			}
			if (libStatus.has("level2_d")) {
				status.getRedCounts()[4] = libStatus.getInt("level2_d");
			}
			if (libStatus.has("level1_u")) {
				status.getGreenCounts()[5] = libStatus.getInt("level1_u");
			}
			if (libStatus.has("level1_d")) {
				status.getRedCounts()[5] = libStatus.getInt("level1_d");
			}
			if (libStatus.has("unknown_u")) {
				status.getGreenCounts()[6] = libStatus.getInt("unknown_u");
			}
			if (libStatus.has("unknown_d")) {
				status.getRedCounts()[6] = libStatus.getInt("unknown_d");
			}
		} catch (NeedPayException ne) {
			//ne.printStackTrace();
			Intent intent = new Intent(activity, com.iwords.android.AlipayActivity.class);
			activity.startActivity(intent);
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			displayMessage(e.getMessage());
			return false;
		}

		return true;
	}
	 */
	
	/**更新单词LearningStatus 到本地数据库*/
	void updateWordStatus(WordStatus word, KnownLevel knownLevel) {
		//if(true) return;
		DBHelper dbHelper = new DBHelper(activity);
		try {
			// 先更新内存中的
			word.setFamiliarity(knownLevel.getValue());
			// 再更新本地数据库中的
			LearningStatus learningStatus = new LearningStatus();
			learningStatus.id = word.getId();
			learningStatus.familiarity = word.getFamiliarity();
			// 标记为待更新到服务器的记录
			learningStatus.persisted = 0;
			dbHelper.update(learningStatus);			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dbHelper.cleanup();
		}
	}
	/**更新单词LearningStatus---到远程服务器版本
	void updateWordStatus(WordStatus word, KnownLevel knownLevel) {
		try {
			// 先更新本地
			word.setFamiliarity(knownLevel.getValue());
			// learningStatus?action=changestatus&userid=?&libid=?&wordid=?&familiarity=?
			// System.out.println("WordsQueueManager.updateWordStatus() word.spelling="+word.getSpelling()+" knownLevel="+knownLevel.name());
			// 再更新服务器端
			String href = iWordsApp.getResources().getString(R.string.user_word_update);
			href = href + "&userid=" + iWordsApp.getUser().getUserid();
			href = href + "&libid=" + word.getLibId();
			href = href + "&wordid=" + word.getWordId();
			href = href + "&familiarity=" + word.getFamiliarity();
			HttpPost request = new HttpPost(href);

			//System.out.println("---WordsQueueManager.updateWordStatus()! spelling = " + word.getSpelling() + " knownLevel=" + knownLevel.name() + " href = " + href);
			HttpResponse httpResponse = new DefaultHttpClient().execute(request);
			String retSrc = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
			// System.out.println(retSrc);
			retSrc = CodeUtils.decode(retSrc);
			JSONObject jsonObject = new JSONObject(retSrc);
			boolean success = jsonObject.getBoolean("isSuccess");
			if (!success) {
				this.displayMessage(CodeUtils.decode(jsonObject.getString("message")));
			}
			// System.out.println("after decode:"+retSrc);
			// JSONObject retJSON = new JSONObject(retSrc);
			// System.out.println("---ShowWord.updateWordStatus()! isSuccess="+retJSON.getBoolean("isSuccess"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	private void displayMessage(String message) {
		//Looper.prepare();
		Toast t1 = Toast.makeText(iWordsApp, message, Toast.LENGTH_LONG);
		t1.show();
	}

}
